﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Tank90
{
    /// <summary>
    ///     类ElementDetail。
    /// </summary>
    [Serializable]
    public class ElementDetail
    {
        public ElementDetail()
        {
        }

        #region Model

        private string _elementtypevch;
        private string _imagepath;
        private int? _transparent;

        /// <summary>
        /// </summary>
        public string ElementTypeVch
        {
            set { _elementtypevch = value; }
            get { return _elementtypevch; }
        }

        /// <summary>
        /// </summary>
        public string ImagePath
        {
            set { _imagepath = value; }
            get { return _imagepath; }
        }

        /// <summary>
        /// </summary>
        public int? Transparent
        {
            set { _transparent = value; }
            get { return _transparent; }
        }

        #endregion Model

        #region  Method

        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public ElementDetail(string ElementTypeVch)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ElementTypeVch,ImagePath,Transparent ");
            strSql.Append(" FROM [ElementDetail] ");
            strSql.Append(" where ElementTypeVch=@ElementTypeVch ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, -1)
            };
            parameters[0].Value = ElementTypeVch;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ElementTypeVch"] != null)
                {
                    this.ElementTypeVch = ds.Tables[0].Rows[0]["ElementTypeVch"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ImagePath"] != null)
                {
                    ImagePath = ds.Tables[0].Rows[0]["ImagePath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Transparent"] != null && ds.Tables[0].Rows[0]["Transparent"].ToString() != "")
                {
                    Transparent = int.Parse(ds.Tables[0].Rows[0]["Transparent"].ToString());
                }
            }
        }

        /// <summary>
        ///     是否存在该记录
        /// </summary>
        public bool Exists(string ElementTypeVch)
        {
            var strSql = new StringBuilder();
            strSql.Append("select count(1) from [ElementDetail]");
            strSql.Append(" where ElementTypeVch=@ElementTypeVch ");

            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, -1)
            };
            parameters[0].Value = ElementTypeVch;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        ///     增加一条数据
        /// </summary>
        public void Add()
        {
            var strSql = new StringBuilder();
            strSql.Append("insert into [ElementDetail] (");
            strSql.Append("ElementTypeVch,ImagePath,Transparent)");
            strSql.Append(" values (");
            strSql.Append("@ElementTypeVch,@ImagePath,@Transparent)");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, 50),
                new SqlParameter("@ImagePath", SqlDbType.VarChar, 200),
                new SqlParameter("@Transparent", SqlDbType.Int, 4)
            };
            parameters[0].Value = ElementTypeVch;
            parameters[1].Value = ImagePath;
            parameters[2].Value = Transparent;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }

        /// <summary>
        ///     更新一条数据
        /// </summary>
        public bool Update()
        {
            var strSql = new StringBuilder();
            strSql.Append("update [ElementDetail] set ");
            strSql.Append("ImagePath=@ImagePath,");
            strSql.Append("Transparent=@Transparent");
            strSql.Append(" where ElementTypeVch=@ElementTypeVch ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ImagePath", SqlDbType.VarChar, 200),
                new SqlParameter("@Transparent", SqlDbType.Int, 4),
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, 50)
            };
            parameters[0].Value = ImagePath;
            parameters[1].Value = Transparent;
            parameters[2].Value = ElementTypeVch;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        ///     删除一条数据
        /// </summary>
        public bool Delete(string ElementTypeVch)
        {
            var strSql = new StringBuilder();
            strSql.Append("delete from [ElementDetail] ");
            strSql.Append(" where ElementTypeVch=@ElementTypeVch ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, -1)
            };
            parameters[0].Value = ElementTypeVch;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        ///     得到一个对象实体
        /// </summary>
        public void GetModel(string ElementTypeVch)
        {
            var strSql = new StringBuilder();
            strSql.Append("select ElementTypeVch,ImagePath,Transparent ");
            strSql.Append(" FROM [ElementDetail] ");
            strSql.Append(" where ElementTypeVch=@ElementTypeVch ");
            SqlParameter[] parameters =
            {
                new SqlParameter("@ElementTypeVch", SqlDbType.VarChar, -1)
            };
            parameters[0].Value = ElementTypeVch;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ElementTypeVch"] != null)
                {
                    this.ElementTypeVch = ds.Tables[0].Rows[0]["ElementTypeVch"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ImagePath"] != null)
                {
                    ImagePath = ds.Tables[0].Rows[0]["ImagePath"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Transparent"] != null && ds.Tables[0].Rows[0]["Transparent"].ToString() != "")
                {
                    Transparent = int.Parse(ds.Tables[0].Rows[0]["Transparent"].ToString());
                }
            }
        }

        /// <summary>
        ///     获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            var strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [ElementDetail] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        #endregion  Method
    }
}